// pages/orders/shoppingList/index.js
const router = require('../../../utils/router.js')
const api = require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    totalAmount: 0,
    goodsList: []
  },
  tapNavigateToUrl({ currentTarget }) {
    router.push(currentTarget.dataset.url)
  },
  countChange ({detail}) {
    let goodsList = [...this.data.goodsList]
    goodsList[detail.index] = detail.goods
    this.setData({
      goodsList: goodsList
    })
    this.computeTotalAmount()
  },
  computeTotalAmount () {
    let totalAmount = 0
    let totalcount = 0
    const goodsList = this.data.goodsList
    for (let i = 0; i < goodsList.length; i++) {
      totalAmount += (goodsList[i].Price * goodsList[i].Number)
      totalcount += goodsList[i].Number
    }
    this.setData({
      totalAmount: totalAmount
    })
    this.saveShopingCartToStorage(totalcount)
  },
  getGoodsList() {
    return new Promise((resolve, reject) => {
      api._get('/User/Design/Goods/List', {
        designInfoID: this.data.orderId
      }).then(res => {
        if (res.Code === 100000) {
          this.setData({
            goodsList: res.Data
          })
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 保存清单到方案
   */
  saveDesignGoods() {
    return new Promise((resolve, reject) => {
      api._put(`/User/Design/Goods/Save?designInfoID=${this.data.orderId}`).then(res => {
        if (res.Code === 100000) {
          wx.redirectTo({
            url: `/pages/orders/designPlanDetail/index?orderId=${this.data.orderId}`
          })
          resolve(res.Data)
        }
      })
    })
  },
  longTapGoods({ currentTarget }) {
    const that = this
    wx.showActionSheet({
      itemList: ['移除'],
      success (res) {
        if (res.tapIndex === 0) {
          that.removeGoods(currentTarget.dataset.goodsincartid, currentTarget.dataset.goodsindex)
        }
      }
    })
  },
  removeGoods(goodsInCartID, goodsindex) {
    return new Promise(resolve => {
      api._delete(`/User/Design/Goods/Delete?goodsInCartID=${goodsInCartID}`).then(res => {
        if (res.Code === 100000) {
          let list = this.data.goodsList
          list.splice(goodsindex, 1)
          this.setData({
            goodsList: list
          })
          this.computeTotalAmount()
        }
      })
    })
  },
  saveShopingCartToStorage(totalcount) {
    wx.setStorageSync('shopingCart', {
      id: this.data.orderId,
      list: this.data.goodsList,
      totalcount: totalcount
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({ orderId }) {
    this.setData({
      orderId: orderId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getGoodsList().then(() => {
      this.computeTotalAmount()
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})